'use strict';
app.controller('shopTemplateCtrl',['$scope','utilService','shopTemplateService','toaster','$timeout','$modal','$log', function($scope,utilService,shopTemplateService,toaster,$timeout,$modal,$log){
    $scope.templates=[{type:1,typography:1,advertise:[],goods:[],navigation:[]},{type:0,typography:0,advertise:[],goods:[],navigation:[],font:{}},{type:2,typography:1,advertise:[],goods:[],navigation:[],font:{}},{type:2,typography:0,advertise:[],goods:[],navigation:[],font:{}}];
    $scope.tombstonesAdvertise=[{},{}];
    $scope.hide=true;

    $scope.changeBox=function(index){
        for(var i=0;i<$scope.templates.length;i++){
            if(index!=i){
                $scope.templates[i].active=false;
            }
        }
        $scope.templates[index].active=!$scope.templates[index].active;
    }


    //打开商品添加弹窗
    $scope.openAddGoods = function (item,$event) {
        $event.stopPropagation();

        var modalInstance = $modal.open({
            templateUrl: 'tpl/shop/shop_template_goods_add.html',
            controller: 'shopTemplateGoodsAddCtrl',
            size: 'md',
            scope:$scope,
            resolve:{
                data:function(){
                    return {item:item};
                }
            }
        });

        modalInstance.result.then(function (result) {
        }, function () {
            $log.info('Modal dismissed at: ' + new Date());
        });
    };

    //打开广告添加弹窗
    $scope.openAddAdvertisement = function (item,$event) {
        $event.stopPropagation();

        var modalInstance = $modal.open({
            templateUrl: 'tpl/shop/shop_template_advertisement_add.html',
            controller: 'shopTemplateAdvertisementAddCtrl',
            size: 'md',
            scope:$scope,
            resolve:{
                deps: ['$ocLazyLoad',
                    function ($ocLazyLoad) {
                        return $ocLazyLoad.load('angularFileUpload').then(
                            function () {
                                return $ocLazyLoad.load([
                                    'css/content-css/upload/picture_upload.css'
                                ]);
                            }
                        );
                }],
                data:function(){
                    return {item:item};
                }
            }
        });

        modalInstance.result.then(function (result) {
        }, function () {
            $log.info('Modal dismissed at: ' + new Date());
        });
    };

    //打开导航图片添加弹窗
    $scope.openAddNavigation = function (item,$event) {
        $event.stopPropagation();

        var modalInstance = $modal.open({
            templateUrl: 'tpl/shop/shop_template_navigation_add.html',
            controller: 'shopTemplateNavigationAddCtrl',
            size: 'md',
            scope:$scope,
            resolve:{
                deps: ['$ocLazyLoad',
                    function ($ocLazyLoad) {
                        return $ocLazyLoad.load('angularFileUpload').then(
                            function () {
                                return $ocLazyLoad.load([
                                    'css/content-css/upload/picture_upload.css'
                                ]);
                            }
                        );
                    }],
                data:function(){
                    return {item:item};
                }
            }
        });

        modalInstance.result.then(function (result) {
        }, function () {
            $log.info('Modal dismissed at: ' + new Date());
        });
    };

    $scope.sortableOptions = {
        // 数据有变化
        update: function(e, ui) {
            //需要使用延时方法，否则会输出原始数据的顺序，可能是BUG？
            $timeout(function() {
                var resArr = [];
                for (var i = 0; i < $scope.templates.length; i++) {
                    resArr.push($scope.templates[i]);
                }
            })


        },
        // 完成拖拽动作
        stop: function(e, ui) {
            //do nothing
        }
    }

    //删除box
    $scope.delBox=function(item,$event){
        $event.stopPropagation();

        var index=$scope.templates.indexOf(item);
        if(index==-1){
            return;
        }
        $scope.templates.splice(index,1);
    }

    //初始化对象
    var initObj=function(type,typography){
        var obj=new Object();
        obj.type=type;
        obj.typography=typography;
        obj.advertise=[];
        obj.goods=[];
        obj.navigation=[];
        obj.font={sizeType:0,locationType:0,content:'请输入文本内容'};
        return obj;
    }

    $scope.addBox=function(type,typography){
        var obj=initObj(type,typography);
        $scope.templates.push(obj);
    }


    //改变排版样式
    $scope.changeTypography=function(item,typography,$event){
        $event.stopPropagation();
        item.typography=typography;
        item.advertise=[];
        item.navigation=[];

    }

    //删除商品
    $scope.deleteGoods=function(item,subitem,$event){
        $event.stopPropagation();
        var index=item.goods.indexOf(subitem);
        if(index!=-1){
            item.goods.splice(index,1);
        }
    }

    //删除广告
    $scope.deleteAdvertise=function(item,subitem,$event){
        $event.stopPropagation();
        var index=item.advertise.indexOf(subitem);
        if(index!=-1){
            item.advertise.splice(index,1);
        }
    }

    //删除导航
    $scope.deleteNavigation=function(item,subitem,$event){
        $event.stopPropagation();
        var index=item.navigation.indexOf(subitem);
        if(index!=-1){
            item.navigation.splice(index,1);
        }
    }

    //添加
    $scope.add=function(){
        shopTemplateService.add($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        })
    }


}]);

app.controller('shopTemplateGoodsAddCtrl', ['$scope', '$modalInstance','toaster','utilService','data', function($scope, $modalInstance,toaster,utilService,data) {

    $scope.goodsListVO={};

    $scope.page = {};
    $scope.page.currentPage = 1;
    //分页
    $scope.replacePage = function () {
        $scope.currentPage = $scope.page.currentPage;
        $scope.pageChanged();
    };

    //查询商品列表
    $scope.findPageAll = function () {
        utilService.queryForPagingList($scope, {
            url: $scope.app.domain + 'goods',
            params: $scope.goodsListVO
        }).then(function (result) {
        });
    }

    $scope.findPageAll();

    //批量选择
    $scope.selected = [];

    $scope.isChecked = function (item) {
        return $scope.selected.indexOf(item) >= 0;
    };

    $scope.updateSelection = function ($event, item) {

        var checkbox = $event.target;
        var checked = checkbox.checked;

        if (checked) {
            $scope.selected.push(item);
        } else {
            var idx = $scope.selected.indexOf(item);
            $scope.selected.splice(idx, 1);
        }
    };

    $scope.isCheckedAll = function () {
        if ($scope.list == null) {
            return;
        }
        return $scope.selected.length == $scope.list.length;
    }

    $scope.checkAll = function ($event) {
        var checkbox = $event.target;
        var checked = checkbox.checked;

        for (var i = 0; i < $scope.list.length; i++) {
            if (checked) {
                $scope.selected.push($scope.list[i]);
            } else {
                $scope.selected.splice(0, $scope.selected.length);
            }
        }
    }

    $scope.initSelect = function () {
        $scope.selected = [];
    }

    //确认
    $scope.ok=function(){
        data.item.goods=data.item.goods.concat($scope.selected);
        $modalInstance.close("关闭");
    }

    //取消
    $scope.cancel = function () {
        $modalInstance.dismiss("关闭");
    };
}]);

app.controller('shopTemplateAdvertisementAddCtrl', ['$scope', '$modalInstance','toaster','data',function($scope, $modalInstance,toaster,data) {
    //队列
    $scope.queue = [];
    $scope.images=new Array();
    $scope.path=$scope.app.image.shop;
    $scope.limit=1;
    $scope.pic="";

    //data
    $scope.ok=function(){
        var obj=new Object();
        obj.pic=$scope.pic;
        data.item.advertise.push(obj);
        $modalInstance.close("关闭");
    }

    //取消
    $scope.cancel = function () {
        $modalInstance.dismiss("关闭");
    };
}]);

app.controller('shopTemplateNavigationAddCtrl', ['$scope', '$modalInstance','toaster','data',function($scope, $modalInstance,toaster,data) {
    //队列
    $scope.queue = [];
    $scope.images=new Array();
    $scope.path=$scope.app.image.shop;
    $scope.limit=1;
    $scope.pic="";

    //data
    $scope.ok=function(){
        var obj=new Object();
        obj.pic=$scope.pic;
        data.item.navigation.push(obj);
        $modalInstance.close("关闭");
    }

    //取消
    $scope.cancel = function () {
        $modalInstance.dismiss("关闭");
    };
}]);
